import { AxiosPromise, AxiosResponse } from 'axios'
import { api } from '../boot/axios'
import { RepairOrder,CurrentLatestVersion, RepairOrderCheckStatus, Parts, CheckInOrder, FinalCheckForm } from './type'

interface checkInPart {
  stock_code: string,
  serial_no: string
}
function url(s: string) {
  return ['/api/repair', s.replace('/', ''), ''].join('/')
}

export function createRepairOrder(repairOrder: CheckInOrder) {
    return api({
      url: url('create_repair_order'),
      method: 'post',
      data: {
        serial_no: repairOrder.serial_no,
        stock_code: repairOrder.stock_code.toUpperCase(),
        work_order_no: repairOrder.work_order_no,
        car: repairOrder.car,
        mtce_type: repairOrder.mtce_type,
        report_date: repairOrder.report_date,
        location: repairOrder.location,
        department: repairOrder.department,
        failure_symptom: repairOrder.failure_symptom,
        eqpt_description: repairOrder.description,
        remark: repairOrder.remark,
        status: repairOrder.status,
        io_counter: repairOrder.io_counter,
        workgroup: repairOrder.workgroup,
        parent_work_order_no: repairOrder.parent_work_order_no,
        contractor_liability: repairOrder.contractor_liability,
        dlc_form_no: repairOrder.dlc_form_no,
        designated_person_name: repairOrder.designated_person_name,
        date: repairOrder.date,
        line:repairOrder.line,
        line_work_order:repairOrder.parent_work_order_no,
        check_in_date:repairOrder.check_in_date,
        equipment:repairOrder.equipment,
        delay_i: repairOrder.delay_i,
        delay_a: repairOrder.delay_a,
        po_no: repairOrder.po_no,
        inspection_no: repairOrder.inspection_no,
        received_date: repairOrder.received_date,
      }
    })
  }



export function getOrderDetail(id:number) {
  return api({
    url: url(`${id}`),
    method: 'get',
  })
}



export function part_fitment(stock_code:string,serial_no:string,parent_stock_code:string,parent_serial_no:string,order_id:number){
  return api({
    url: url('part_detail_fitment'),
    method: 'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_no:serial_no,
      parent_stock_code:parent_stock_code.toUpperCase(),
      parent_serial_no:parent_serial_no,
      order_id
    }
  })
}

export function part_defitment(stock_code:string,serial_no:string,order_id:number){
  return api({
    url: url('part_detail_defitment'),
    method: 'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_no:serial_no,
      order_id
    }
  })
}


export function getPartDetail(stock_code:string){
  return api({
    url: url('get_part_detail'),
    method: 'post',
    data: {
      stock_code,
    }
  })
}

export function getPartsOrderHistory(repair_order_id:number){
  return api({
    url: url('get_maintenance_history'),
    method: 'post',
    data: {
      repair_order_id
    }
  })
}

export async function getAllVersion(repair_order_id:number){
  const res = await api({
    url: url('get_all_version'),
    method: 'post',
    data: {
      repair_order_id
    }
  }) 
  return res.data.data as CurrentLatestVersion[]
}


export async function getCheckStatus(repair_order_id:number){
  const res = await api({
    url: url('check_status'),
    method: 'post',
    data: {
      repair_order_id
    }
  }) 
  return res.data as RepairOrderCheckStatus
}

export function searchOrderParts(
  stock_code:string,
  serial_no:string,
  checkin_status:boolean,
  checkout_status:boolean,
  complete_status:boolean,
  status: string,
  mtce_type: string,
  short_description: string,
){
  return api({
    url: url('get_order_parts'),
    method: 'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_no:serial_no,
      checkin_status:checkin_status,
      checkout_status:checkout_status,
      complete_status:complete_status,
      mtce_type: mtce_type,
      status: status,
      short_description: short_description
    }
  })
}

export function searchCanCheckoutParts(
  stock_code:string,
  serial_no:string
){
  return api({
    url: url('get_can_checkout_parts'),
    method: 'post',
    data: {
      stock_code:stock_code.toUpperCase(),
      serial_no:serial_no
    }
  })
}

export function deleteOrderReplacementTask(
  replacement_task_id:number,
){
  return api({
    url: url('delete_order_replacement_task'),
    method: 'post',
    data: {
      replacement_task_id:replacement_task_id
    }
  })
}

export function getPrimaryPart(repair_order_id:number):AxiosPromise<Parts>{
  return api({
    url: url('get_primary_part'),
    method: 'post',
    data: {
      repair_order_id
    }
  }) 
}

export function jobComplete(repair_order_id:number,force=false,qrcode:string|null=null,username:string|null=null,password:string|null=null){
  return api({
    url: url('job_complete'),
    method: 'post',
    data: {
      repair_order_id,
      force,
      qrcode,
      username,
      password
    }
  }) 
}
export function getOrderParts(repair_order_id: number){
  return api({
    url: url('get_repair_order_parts'),
    method: 'post',
    data:{
      repair_order_id
    }
  }) 
}


export function orderPartFitment(repair_order_id:number,repair_order_part_id: number, serial_no:string){
  return api({
    url: url('fitment'),
    method: 'post',
    data:{
      repair_order_id,
      repair_order_part_id,
      serial_no
    }
  }) 
}

export function orderPartDefitment(repair_order_part_id: number){
  return api({
    url: url('defitment'),
    method: 'post',
    data:{
      repair_order_part_id,
    }
  }) 
}

export function getInputOption(){
  return api({
    url: url('input_option'),
    method: 'get',
  }) 
}

export function createOrderHtreplace(repair_order_part_id:number,repair_order_id:number,component:string){
  return api({
    url: url('create_replacement_task'),
    method: 'post',
    data: {
      repair_order_part_id,
      repair_order_id,
      component
    }
  }) 
}


export function confirmRepairOrder(repair_order_id:number, check: number){
  return api({
    url: url('confirm_repair_order'),
    method: 'post',
    data: {
      repair_order_id,
      check
    }
  })
}

export function checkoutOrder(repair_order_id: number, workgroup:string){
  return api({
    url: url('checkout'),
    method: 'post',
    data: {
      repair_order_id,
      workgroup
    }
  })
}

export function updateReplacementTask(repair_order_replacement_id: number, completed: boolean, update_time:string){
  return api({
    url: url('update_replacement_task'),
    method: 'post',
    data:{
      repair_order_replacement_id,
      completed,
      update_time
    }
  }) 
}

export function getOrderHardTimeReplacement(repair_order_id:number){
  return api({
    url: url('get_hard_time_replacement'),
    method: 'post',
    data:{
      repair_order_id 
    }
  }) 
}

export function updateOrderFinalCheckForm(data:FinalCheckForm){
  console.log(data.repeat_within_six_months);
  
  return api({
    url: url('update_repair_order_details'),
    method: 'post',
    data:{
      repair_order_id: data.repair_order_id,
      repair_details: data.repair_details,
      fault_equipment: data.fault_equipment,
      failure_symptom: data.failure_symptom,
      actions: data.actions,
      remarks: data.remarks,
      delivery_to: data.delivery_to,
      maintenance_type: data.maintenance_type,
      scrapped: data.scrapped,
      deleted: data.deleted,
      test_result: data.test_result,
      maintenance_details: data.maintenance_details,
      spare_part: data.spare_part,
      repeat_within_six_months: data.repeat_within_six_months,
    }
  }) 
}

export function getFinalCheckForm(order_id:number){
  return api({
    url: `/api/repair/get_repair_order_details`,
    params:{
      repair_order_id:order_id
    },
    method: 'get',
  }) 
}

export function getOrderDetailData(order_id: number){
  return api({
    url: `/api/repair/${order_id}`,
    method: 'get',
  }) 
}

export function getOrderDetailLabel2Data(repair_order_id: number){
  return api({
    url: url('get_contractor_liability'),
    method: 'post',
    data: {
      repair_order_id
    }
  }) 
}

export function deleteStockItemHtreplace(replacement_task_id:number){
  return api({
    url: url('delete_order_replacement_task'),
    method: 'post',
    data: {
      replacement_task_id
    },
  });
}

export function getOrderCheckout(repair_order_id: number){
  // return api({
  //   url: url('get_order_checkout'),
  //   method: 'post',

  // });
  console.log(repair_order_id);
  
}


export function getOrderChildPartsList(stock_code: string, serial_no: string, repair_order_id: number){
  return api({
    url:url('get_child_parts'),
    method: 'post',
    data:{
      parent_stock_code:stock_code.toUpperCase(),
      parent_serial_no: serial_no,
      repair_order_id
    }
  })
}

export function patchRepairOrder(data:Partial<CheckInOrder>={} ) {
  return api({
    url: url(`${data.id}`),
    method: 'patch',
    data
  })
}

export function checkPartIsExist(data:checkInPart) {
  const { stock_code, serial_no } = data;
  return api({
    url: url('check_create_parts_order_exists'),
    method: 'post',
    data: {
      stock_code,
      serial_no
    }
  })
}
